package com.haoshuang.zhouzhoubang.ui.activity


import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import android.widget.TextView

import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter4.BaseQuickAdapter
import com.haoshuang.zhouzhoubang.R
import com.haoshuang.zhouzhoubang.databinding.ActivityServiceRecordBinding
import com.haoshuang.zhouzhoubang.databinding.ItemServiceRecordBinding
import com.haoshuang.zhouzhoubang.model.bean.OrderBean
import com.haoshuang.zhouzhoubang.ui.vm.ServiceRecordViewModel
import com.haoshuang.zhouzhoubang.utils.NavigationUtils
import com.mufeng.libs.base.BaseBindingHolder
import com.mufeng.libs.base.BaseBindingQuickAdapter
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.core.setOnItemChildClick
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.callPhone
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.load
import com.mufeng.libs.utils.view.visible


/**
 *  服务记录界面
 */
class ServiceRecordActivity :
    BaseVMActivity<ServiceRecordViewModel, ActivityServiceRecordBinding>() {
    /**recycleview列表适配器*/
    private val mrvAdapter by lazy {
        object : BaseBindingQuickAdapter<OrderBean, ItemServiceRecordBinding>(
            mOrderList
        ) {
            override fun onBindViewHolder(
                holder: BaseBindingHolder,
                position: Int,
                item: OrderBean?
            ) {
                if (item == null) return
                val binding = ItemServiceRecordBinding.bind(holder.itemView)
                binding.apply {
                    tvName.text = item.real_name
                    try {
                        var phonenum = item.user_phone
                        //方法二
                        phonenum = phonenum.substring(0, 3) + "****" + phonenum.substring(7, 11);

                        userPhone.text = phonenum
                    } catch (e: Exception) {
                        userPhone.text = item.user_phone
                    }
                    serviceTime.text = "期待上门时间：" + item.service_time
                    orderId.text = "订单编号：" + item.order_id
                    storeName.text = "服务内容：" + item.store_name
                    textView29.text = item.user_address
                    tvMark.text = item.mark

                    ivHead.load(item.user_avatar, R.mipmap.rl)
                    if (item.status == "3") {
                        tvCall.gone()
                        imageView12.gone()
                    } else {
                        tvCall.visible()
                        imageView12.visible()
                    }
                    //（0 ：待支付 1：待服务；2：服务中；3：已完成；4：已取消；5 待接单；6 验收中 ）
                    when (item.status) {
                        "0" -> {
                            //待服务
                            tvStatus.text = "待支付"

                        }

                        "1" -> {
                            //待服务
                            tvStatus.text = "待服务"

                        }

                        "2" -> {
                            tvStatus.text = "服务中"

                        }

                        "3" -> {
                            tvStatus.text = "已完成"

                        }

                        "5" -> {
                            tvStatus.text = "待接单"

                        }

                        "6" -> {
                            tvStatus.text = "验收中"

                        }

                        else -> {

                            tvStatus.text = "已取消"
                        }


                    }
                }
            }
        }
    }
    var isCheck9 = 0
    override fun initView(savedInstanceState: Bundle?) {
        val layoutManager = LinearLayoutManager(this)    // 创建布局管理者
        binding.apply {
            appBar.ivBack.clickWithTrigger { finish() }
            appBar.tvTitle.text = "服务记录"

            llSx.clickWithTrigger {
                if (isCheck9 == 0) {
                    isCheck9 = 1
                    llSx2.visible()
                    ivSx.setImageResource(R.mipmap.hj8)
                    tvSx.setTextColor(Color.parseColor("#00B7F5"))
                } else {
                    isCheck9 = 0
                    ivSx.setImageResource(R.mipmap.hj7)
                    llSx2.gone()
                    tvSx.setTextColor(Color.parseColor("#333333"))
                }

            }
            tvSs.setOnEditorActionListener { v, actionId, event ->
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    keyword = tvSs.text.toString()
                    if (keyword.isNullOrBlank()) {
                        toast("请输入搜索内容")
                    } else {
                        p = 1
                        getNet()
                    }
                }
                false;
            }


            tvGb.clickWithTrigger {
                if (status9 != -1) {
                    p = 1
                    getNet()
                }
                isCheck9 = 0
                ivSx.setImageResource(R.mipmap.hj7)
                llSx2.gone()
                tvSx.setTextColor(Color.parseColor("#333333"))
            }
            tvQr.clickWithTrigger {

                if (status9 != -1) {
                    p = 1
                    getNet()
                    isCheck9 = 0
                    ivSx.setImageResource(R.mipmap.hj7)
                    llSx2.gone()
                    tvSx.setTextColor(Color.parseColor("#333333"))
                } else {
                    toast("请选择")
                }

            }
            tvText1.clickWithTrigger {
                status9 = 2
                //  tvText1.setBackgroundResource(R.drawable.bg_4xz)
                //  tvText2.setBackgroundResource(R.drawable.bg_4wxz)
                tvText3.setBackgroundResource(R.drawable.bg_4wxz)
                tvText4.setBackgroundResource(R.drawable.bg_4wxz)
                tvText5.setBackgroundResource(R.drawable.bg_4wxz)
                tvText6.setBackgroundResource(R.drawable.bg_4wxz)

            }
            tvText2.clickWithTrigger {
                status9 = 2
                //  tvText1.setBackgroundResource(R.drawable.bg_4wxz)
                //  tvText2.setBackgroundResource(R.drawable.bg_4xz)
                tvText3.setBackgroundResource(R.drawable.bg_4wxz)
                tvText4.setBackgroundResource(R.drawable.bg_4wxz)
                tvText5.setBackgroundResource(R.drawable.bg_4wxz)
                tvText6.setBackgroundResource(R.drawable.bg_4wxz)
            }
            tvText3.clickWithTrigger {
                status9 = 2
                //  tvText1.setBackgroundResource(R.drawable.bg_4wxz)
                //   tvText2.setBackgroundResource(R.drawable.bg_4wxz)
                tvText3.setBackgroundResource(R.drawable.bg_4xz)
                tvText4.setBackgroundResource(R.drawable.bg_4wxz)
                tvText5.setBackgroundResource(R.drawable.bg_4wxz)
                tvText6.setBackgroundResource(R.drawable.bg_4wxz)
            }
            tvText4.clickWithTrigger {
                status9 = 6
                //  tvText1.setBackgroundResource(R.drawable.bg_4wxz)
                //  tvText2.setBackgroundResource(R.drawable.bg_4wxz)
                tvText3.setBackgroundResource(R.drawable.bg_4wxz)
                tvText4.setBackgroundResource(R.drawable.bg_4xz)
                tvText5.setBackgroundResource(R.drawable.bg_4wxz)
                tvText6.setBackgroundResource(R.drawable.bg_4wxz)
            }
            tvCancle.clickWithTrigger {
                status9 = -1
                //  tvText1.setBackgroundResource(R.drawable.bg_4wxz)
                //  tvText2.setBackgroundResource(R.drawable.bg_4wxz)
                tvText3.setBackgroundResource(R.drawable.bg_4wxz)
                tvText4.setBackgroundResource(R.drawable.bg_4wxz)
                tvText5.setBackgroundResource(R.drawable.bg_4wxz)
                tvText6.setBackgroundResource(R.drawable.bg_4wxz)
            }
            tvText5.clickWithTrigger {
                status9 = 3
                // tvText1.setBackgroundResource(R.drawable.bg_4wxz)
                //  tvText2.setBackgroundResource(R.drawable.bg_4wxz)
                tvText3.setBackgroundResource(R.drawable.bg_4wxz)
                tvText4.setBackgroundResource(R.drawable.bg_4wxz)
                tvText5.setBackgroundResource(R.drawable.bg_4xz)
                tvText6.setBackgroundResource(R.drawable.bg_4wxz)
            }
            tvText6.clickWithTrigger {
                status9 = 4
                // tvText1.setBackgroundResource(R.drawable.bg_4wxz)
                //  tvText2.setBackgroundResource(R.drawable.bg_4wxz)
                tvText3.setBackgroundResource(R.drawable.bg_4wxz)
                tvText4.setBackgroundResource(R.drawable.bg_4wxz)
                tvText5.setBackgroundResource(R.drawable.bg_4wxz)
                tvText6.setBackgroundResource(R.drawable.bg_4xz)
            }
            recyclerView.layoutManager = layoutManager    // 给recycleview设置布局管理者
            recyclerView.adapter = mrvAdapter    // 给recycleview设置适配器对象
            mrvAdapter.setOnItemChildClick(id = R.id.tvCall) { view, position, item ->
                if (item?.user_phone.isNullOrBlank()) {
                    toast("电话不能为空")
                }
                callPhone(item?.user_phone.toString())
            }
            mrvAdapter.setOnItemChildClick(id = R.id.imageView12) { view, position, item ->
                if (item == null) return@setOnItemChildClick
                if (item.lat.isBlank()) {
                    toast("经纬度不能为空")
                    return@setOnItemChildClick
                }
                NavigationUtils.jumpNavigation(
                    this@ServiceRecordActivity,
                    "JUMP_TEN_XUN",
                    item.lat,
                    item.lng,
                    item.user_address
                )
            }

            mrvAdapter.setOnItemClickListener { _, _, position ->
                var str = ""
                when (mOrderList[position].status) {
                    "0" -> {
                        str = "待支付"
                    }

                    "1" -> {
                        if (mOrderList[position].is_confirm == "0") {
                            str = "待确认"
                        } else {
                            str = "待上门"
                        }
                    }

                    "2" -> {
                        str = "服务中"
                    }

                    "6" -> {
                        str = "验收中"
                    }

                    "5" -> {
                        str = "待接单"
                    }

                    "3" -> {
                        str = "已完成"
                    }

                    else -> {
                        if (mOrderList[position].progress == 0) {
                            str = "已取消"
                        } else {
                            str = "退款中"
                        }
                    }

                }
//                startPage<ServiceOrderStatusActivity>(
//                    "ids" to mOrderList[position].id,
//                    "str" to str
//                )
                startPage<ServiceLogActivity>("ids" to mOrderList[position].id)
            }
        }
    }

    private var mOrderList = mutableListOf<OrderBean>()
    override fun initData() {
        binding.refreshLayout.setOnRefreshListener {
            p = 1
            getNet()
            binding.refreshLayout.finishRefresh(2000)
            binding.refreshLayout.setEnableLoadMore(true)
        }
        binding.refreshLayout.setOnLoadMoreListener {
            if (mOrderList.size < 10) {
                binding.refreshLayout.setEnableLoadMore(false)
            } else {
                p++
                getNet()
            }
            binding.refreshLayout.finishLoadMore(2000)
        }
        p = 1
        getNet()
    }

    var status9 = -1
    var keyword = ""
    private fun getNet() {
        if (status9 != -1) {
            vm.getData( "p" to p, "status" to status9, "keyword" to keyword)

        } else {
            vm.getData("p" to p, "keyword" to keyword)
        }

    }

    override fun startObserve() {

        vm.getData.observeState(this) {
            onLoading { }
            onError { code, msg ->
                dismissLoading()
                if (code != "402") {
                    toast(msg)
                }
            }
            onSuccess {
                dismissLoading()
                if (p == 1)
                    mOrderList.clear()
                mOrderList.addAll(it!!)
                mrvAdapter.notifyDataSetChanged()
            }
        }
    }

}